package com.cssl.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cssl.pojo.Dept;
import com.cssl.pojo.Emp;
import com.cssl.service.DeptService;
import com.cssl.service.EmpService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private DeptService deptService;


    public EmpController(){
        System.out.println("EmpController");
    }

    @ResponseBody
    @GetMapping("/map")
    public List<Map<String, Object>> selectMap() {
        System.out.println("map");
        System.out.println("map2");
        return empService.select();
    }

    /**
     * 新增员工
     * @return
     */
    @GetMapping("/add")
    public String add(Model model){
        System.out.println("addindex");
        List<Dept> list=deptService.list();
        model.addAttribute("deptlist",list);
        return "add";
    }
    /**
     * 回显员工
     * @return
     */
    @GetMapping("/detail/{empno}")
    public String detail(Model model,@PathVariable int empno){
        System.out.println("detail:"+empno);
        Emp emp=empService.getById(empno);
        System.out.println(emp);
        model.addAttribute("emp",emp);
        model.addAttribute("deptlist",deptService.list());
        return "detail";
    }


    /**
     * 增加员工
     * @return
     */
    @PostMapping("/addemp")
    public String adddept(Emp emp,Model model) {
        boolean res = empService.save(emp);
        System.out.println("res:"+res);
        if (res){
            model.addAttribute("pageindex",1);
            model.addAttribute("pagecount",empService.selectCount());
            //model.addAttribute("maps",empService.selectMap(1,3));
            return "thyme";
        }
        model.addAttribute("emp",emp);
        model.addAttribute("deptlist",deptService.list());
        return "add";
    }

    /**
     * 修改员工
     * @return
     */
    @PostMapping("/update")
    public String update(Emp emp,Model model) {
        System.out.println("update");
        boolean res =empService.updateById(emp);
        System.out.println("res:"+res);
        if (res){
            model.addAttribute("pageindex",1);
            model.addAttribute("pagecount",empService.selectCount());
           // model.addAttribute("maps",empService.selectMap(1,3));
            return "thyme";
        }
        model.addAttribute("deptlist",deptService.list());
        return "detail";
    }

    /**
     * 分页
     * @return
     */
    @GetMapping("/thyme/{pageindex}")
    public String thymeleaf(Model model,@PathVariable int pageindex){
        //Page<Map<String, Object>> maps = empService.selectMap(pageindex,3);
        System.out.println("pageindex:"+pageindex);
       // model.addAttribute("maps",maps);
        model.addAttribute("pageindex",pageindex);
        model.addAttribute("pagecount",empService.selectCount());
        return "thyme";
    }

    /**
     * 分页
     * @return
     */
   // @ResponseBody
    @GetMapping("/thyme2/{pageindex}")
    public String thyme(@PathVariable int pageindex,Model model){
       System.out.println("分页2:"+pageindex);
        IPage<Emp> page = empService.findAll(new Page<Emp>(pageindex,3));
       /* //  Page<Map<String, Object>> maps = empService.selectMap(pageindex,3);
        Page<Emp> pageArt=new Page<Emp>(pageindex,3);
        System.out.println("size"+pageArt.size());
        Page<Emp> page1 = empService.findAll(pageArt);
        System.out.println("page1"+page1.size());
        int total = (int)page1.getTotal();
        // return TableMap.ResultJson(0,total,"ok",page1.getRecords()); //layui table 解析返回格式
*/
       model.addAttribute("maps",page);
        System.out.println("page:"+page.getSize());
        model.addAttribute("pageindex",pageindex);
        model.addAttribute("pagecount",empService.selectCount());
        return "thyme";
    }

    /**
     * 所有员工
     * @param model
     * @return
     */
    @GetMapping("/show")
    public String select(Model model) {
        List<Map<String, Object>> select = empService.select();
       // E page = empService.page(new Page<Emp>(1, 3));

        model.addAttribute("maps",select);
        //model.addAttribute("mess","员工表信息");
        return "thyme";
    }
}
